<?php





//数目

function invested_projects_information_invested_numbers_table($fund_id){
    $query = db_select("invested_projects_information_invested_numbers", 'f');
    $query->fields('f',
        array(
            'id',
            'fund_id',

            'invested_number',

            'invested_number_on_tsingdao',

            'invested_number_not_on_tsingdao',

            'invested_number_on_early_tsingdao',

            'invested_number_not_on_early_tsingdao',

        ));
    if($fund_id > 0){
        $query->condition("f.fund_id", $fund_id, '=');
    }
    $result = $query->execute();
    $rows = array();


    $header = array(
        array('data' => "公司名称"),

        array('data' => "投资数目"),

        array('data' => "已投青岛数"),

        array('data' => "已投外地数"),

        array('data' => "青岛早中期数"),

        array('data' => "非青岛早中期数"),

    );
    $count = 0;
    foreach($result as $row){
        $count++;
        $fund_query = db_select("fund_basic_information","f")->fields("f",array("registered_name"))
            ->condition("f.id", $row->fund_id, "=");
        $fund_result = $fund_query->execute();
        $fund = NULL;
        foreach($fund_result as $fund_row){
            $fund = $fund_row;
        }
        $rows[] = array(
            'data' => array(
                'fund_name' => $fund->registered_name,

                'invested_number' => $row->invested_number,

                'invested_number_on_tsingdao' => $row->invested_number_on_tsingdao,

                'invested_number_not_on_tsingdao' => $row->invested_number_not_on_tsingdao,

                'invested_number_on_early_tsingdao' => $row->invested_number_on_early_tsingdao,

                'invested_number_not_on_early_tsingdao' => $row->invested_number_not_on_early_tsingdao,

            )
        );
    }

    $html = "<div><h3>数目信息</h3></div>";
    $html .= theme('table',
        array(
            'header' => $header,
            'rows'=>$rows,
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有信息',//Optional empty text for the table if resultset is empty
        )
    );
    global $user;
    if(!in_array("引导基金管理人员", $user->roles))
    {
        if($count > 0){
            $html .= l("编辑信息", "projectstatistic/fillform/numbers/".$fund_id);
        }else{
            $html .= l("创建信息", "projectstatistic/fillform/numbers/".$fund_id);
        }
    }

    return $html;
}



//金额

function invested_projects_information_invested_value_table($fund_id){
    $query = db_select("invested_projects_information_invested_value", 'f');
    $query->fields('f',
        array(
            'id',
            'fund_id',

            'invested_value',

            'invested_value_on_tsingdao',

            'invested_value_not_on_tsingdao',

            'invested_value_on_early_tsingdao',

            'invested_value_not_on_early_tsingdao',

        ));
    if($fund_id > 0){
        $query->condition("f.fund_id", $fund_id, '=');
    }
    $result = $query->execute();
    $rows = array();


    $header = array(
        array('data' => "公司名称"),

        array('data' => "投资金额"),

        array('data' => "已投青岛金额"),

        array('data' => "已投外地金额"),

        array('data' => "青岛早中期金额"),

        array('data' => "非青岛或非早中期金额"),

    );
    $count = 0;
    foreach($result as $row){
        $count++;
        $fund_query = db_select("fund_basic_information","f")->fields("f",array("registered_name"))
            ->condition("f.id", $row->fund_id, "=");
        $fund_result = $fund_query->execute();
        $fund = NULL;
        foreach($fund_result as $fund_row){
            $fund = $fund_row;
        }
        $rows[] = array(
            'data' => array(
                'fund_name' => $fund->registered_name,

                'invested_value' => $row->invested_value,

                'invested_value_on_tsingdao' => $row->invested_value_on_tsingdao,

                'invested_value_not_on_tsingdao' => $row->invested_value_not_on_tsingdao,

                'invested_value_on_early_tsingdao' => $row->invested_value_on_early_tsingdao,

                'invested_value_not_on_early_tsingdao' => $row->invested_value_not_on_early_tsingdao,

            )
        );
    }

    $html = "<div><h3>金额信息</h3></div>";
    $html .= theme('table',
        array(
            'header' => $header,
            'rows'=>$rows,
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有信息',//Optional empty text for the table if resultset is empty
        )
    );
    global $user;
    if(!in_array("引导基金管理人员", $user->roles))
    {
        if($count > 0){
            $html .= l("编辑信息", "projectstatistic/fillform/value/".$fund_id);
        }else{
            $html .= l("创建信息", "projectstatistic/fillform/value/".$fund_id);
        }
    }

    return $html;
}



//最低标准

function invested_projects_information_minimum_standard_table($fund_id){
    $query = db_select("invested_projects_information_minimum_standard", 'f');
    $query->fields('f',
        array(
            'id',
            'fund_id',

            'target_percentage_on_early_tsingdao',

            'target_value',

            'is_fulfilled',

            'non_restrained_value',

            'is_overflow',

        ));
    if($fund_id > 0){
        $query->condition("f.fund_id", $fund_id, '=');
    }
    $result = $query->execute();
    $rows = array();


    $header = array(
        array('data' => "公司名称"),

        array('data' => "应投资青岛早中期比例"),

        array('data' => "应投资额"),

        array('data' => "是否已投够"),

        array('data' => "非限制金额"),

        array('data' => "是否已超限制"),

    );
    $count = 0;
    foreach($result as $row){
        $count++;
        $fund_query = db_select("fund_basic_information","f")->fields("f",array("registered_name"))
            ->condition("f.id", $row->fund_id, "=");
        $fund_result = $fund_query->execute();
        $fund = NULL;
        foreach($fund_result as $fund_row){
            $fund = $fund_row;
        }
        $rows[] = array(
            'data' => array(
                'fund_name' => $fund->registered_name,

                'target_percentage_on_early_tsingdao' => $row->target_percentage_on_early_tsingdao,

                'target_value' => $row->target_value,

                'is_fulfilled' => $row->is_fulfilled,

                'non_restrained_value' => $row->non_restrained_value,

                'is_overflow' => $row->is_overflow,

            )
        );
    }

    $html = "<div><h3>最低标准信息</h3></div>";
    $html .= theme('table',
        array(
            'header' => $header,
            'rows'=>$rows,
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有信息',//Optional empty text for the table if resultset is empty
        )
    );
    global $user;
    if(!in_array("引导基金管理人员", $user->roles))
    {
        if($count > 0){
            $html .= l("编辑信息", "projectstatistic/fillform/standard/".$fund_id);
        }else{
            $html .= l("创建信息", "projectstatistic/fillform/standard/".$fund_id);
        }
    }

    return $html;
}



//时期

function invested_projects_information_period_statistic_table($fund_id){
    $query = db_select("invested_projects_information_period_statistic", 'f');
    $query->fields('f',
        array(
            'id',
            'fund_id',

            'target_percentage_on_early_tsingdao',

            'target_value',

            'is_fulfilled',

            'value_surplus',

            'non_restrained_value',

            'is_overflow',

        ));
    if($fund_id > 0){
        $query->condition("f.fund_id", $fund_id, '=');
    }
    $result = $query->execute();
    $rows = array();


    $header = array(
        array('data' => "公司名称"),

        array('data' => "应投资青岛早中期比例"),

        array('data' => "应投资额"),

        array('data' => "是否已投够"),

        array('data' => "超过数额"),

        array('data' => "非限制金额"),

        array('data' => "是否已超限制"),

    );
    $count = 0;
    foreach($result as $row){
        $count++;
        $fund_query = db_select("fund_basic_information","f")->fields("f",array("registered_name"))
            ->condition("f.id", $row->fund_id, "=");
        $fund_result = $fund_query->execute();
        $fund = NULL;
        foreach($fund_result as $fund_row){
            $fund = $fund_row;
        }
        $rows[] = array(
            'data' => array(
                'fund_name' => $fund->registered_name,

                'target_percentage_on_early_tsingdao' => $row->target_percentage_on_early_tsingdao,

                'target_value' => $row->target_value,

                'is_fulfilled' => $row->is_fulfilled,

                'value_surplus' => $row->value_surplus,

                'non_restrained_value' => $row->non_restrained_value,

                'is_overflow' => $row->is_overflow,

            )
        );
    }

    $html = "<div><h3>时期信息</h3></div>";
    $html .= theme('table',
        array(
            'header' => $header,
            'rows'=>$rows,
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有信息',//Optional empty text for the table if resultset is empty
        )
    );
    global $user;
    if(!in_array("引导基金管理人员", $user->roles))
    {
        if($count > 0){
            $html .= l("编辑信息", "projectstatistic/fillform/period/".$fund_id);
        }else{
            $html .= l("创建信息", "projectstatistic/fillform/period/".$fund_id);
        }
    }

    return $html;
}



//地域

function invested_projects_information_location_statistic_table($fund_id){
    $query = db_select("invested_projects_information_location_statistic", 'f');
    $query->fields('f',
        array(
            'id',
            'fund_id',

            'target_percentage_on_tsingdao',

            'target_value',

            'location_offset_value',

            'period_offset_value',

            'actual_target_value',

            'is_fulfilled',

            'restrained_value',

            'is_overflow',

            'value_surplus',

        ));
    if($fund_id > 0){
        $query->condition("f.fund_id", $fund_id, '=');
    }
    $result = $query->execute();
    $rows = array();


    $header = array(
        array('data' => "公司名称"),

        array('data' => "应投资青岛比例"),

        array('data' => "应投资额"),

        array('data' => "地域冲抵金额"),

        array('data' => "时期冲抵金额"),

        array('data' => "实际应投金额"),

        array('data' => "是否已投够"),

        array('data' => "限制外金额"),

        array('data' => "是否已超限制"),

        array('data' => "超过数额"),

    );
    $count = 0;
    foreach($result as $row){
        $count++;
        $fund_query = db_select("fund_basic_information","f")->fields("f",array("registered_name"))
            ->condition("f.id", $row->fund_id, "=");
        $fund_result = $fund_query->execute();
        $fund = NULL;
        foreach($fund_result as $fund_row){
            $fund = $fund_row;
        }
        $rows[] = array(
            'data' => array(
                'fund_name' => $fund->registered_name,

                'target_percentage_on_tsingdao' => $row->target_percentage_on_tsingdao,

                'target_value' => $row->target_value,

                'location_offset_value' => $row->location_offset_value,

                'period_offset_value' => $row->period_offset_value,

                'actual_target_value' => $row->actual_target_value,

                'is_fulfilled' => $row->is_fulfilled,

                'restrained_value' => $row->restrained_value,

                'is_overflow' => $row->is_overflow,

                'value_surplus' => $row->value_surplus,

            )
        );
    }

    $html = "<div><h3>地域信息</h3></div>";
    $html .= theme('table',
        array(
            'header' => $header,
            'rows'=>$rows,
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有信息',//Optional empty text for the table if resultset is empty
        )
    );
    global $user;
    if(!in_array("引导基金管理人员", $user->roles))
    {
        if($count > 0){
            $html .= l("编辑信息", "projectstatistic/fillform/location/".$fund_id);
        }else{
            $html .= l("创建信息", "projectstatistic/fillform/location/".$fund_id);
        }
    }

    return $html;
}



//领域

function invested_projects_information_fields_statistic_table($fund_id){
    $query = db_select("invested_projects_information_fields_statistic", 'f');
    $query->fields('f',
        array(
            'id',
            'fund_id',

            'target_percentage_on_fields',

            'target_value',

            'invested_value',

            'is_fulfilled',

            'non_restrained_value',

            'invested_non_restrained_value',

            'is_overflow',

            'fields',

        ));
    if($fund_id > 0){
        $query->condition("f.fund_id", $fund_id, '=');
    }
    $result = $query->execute();
    $rows = array();


    $header = array(
        array('data' => "公司名称"),

        array('data' => "应投资领域比例"),

        array('data' => "应投资额"),

        array('data' => "已投资该领域额"),

        array('data' => "是否已投够"),

        array('data' => "非限制金额"),

        array('data' => "已投非限金额"),

        array('data' => "是否已超限制"),

        array('data' => "领域"),

    );
    $count = 0;
    foreach($result as $row){
        $count++;
        $fund_query = db_select("fund_basic_information","f")->fields("f",array("registered_name"))
            ->condition("f.id", $row->fund_id, "=");
        $fund_result = $fund_query->execute();
        $fund = NULL;
        foreach($fund_result as $fund_row){
            $fund = $fund_row;
        }
        $rows[] = array(
            'data' => array(
                'fund_name' => $fund->registered_name,

                'target_percentage_on_fields' => $row->target_percentage_on_fields,

                'target_value' => $row->target_value,

                'invested_value' => $row->invested_value,

                'is_fulfilled' => $row->is_fulfilled,

                'non_restrained_value' => $row->non_restrained_value,

                'invested_non_restrained_value' => $row->invested_non_restrained_value,

                'is_overflow' => $row->is_overflow,

                'fields' => $row->fields,

            )
        );
    }

    $html = "<div><h3>领域信息</h3></div>";
    $html .= theme('table',
        array(
            'header' => $header,
            'rows'=>$rows,
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有信息',//Optional empty text for the table if resultset is empty
        )
    );
    global $user;
    if(!in_array("引导基金管理人员", $user->roles))
    {
        if($count > 0){
            $html .= l("编辑信息", "projectstatistic/fillform/field/".$fund_id);
        }else{
            $html .= l("创建信息", "projectstatistic/fillform/field/".$fund_id);
        }
    }

    return $html;
}





function projects_statistic_admin_list_view_number(){
    $html = '';

    $html .= invested_projects_information_invested_numbers_table(0);

    $render_array = array(
        '#markup' => $html,
    );

    return $render_array;
}

function projects_statistic_admin_list_view_values(){
    $html = '';

    $html .= invested_projects_information_invested_value_table(0);

    $render_array = array(
        '#markup' => $html,
    );

    return $render_array;
}

function projects_statistic_admin_list_view_minimum_standard(){
    $html = '';

    $html .= invested_projects_information_minimum_standard_table(0);

    $render_array = array(
        '#markup' => $html,
    );

    return $render_array;
}

function projects_statistic_admin_list_view_period(){
    $html = '';

    $html .= invested_projects_information_period_statistic_table(0);

    $render_array = array(
        '#markup' => $html,
    );

    return $render_array;
}

function projects_statistic_admin_list_view_location(){
    $html = '';

    $html .= invested_projects_information_location_statistic_table(0);

    $render_array = array(
        '#markup' => $html,
    );

    return $render_array;
}

function projects_statistic_admin_list_view_field(){
    $html = '';

    $html .= invested_projects_information_fields_statistic_table(0);

    $render_array = array(
        '#markup' => $html,
    );

    return $render_array;
}
function projects_statistic_main() {
    drupal_set_title('基金信息管理');
    $render_array = array(
        '#markup' => '',
    );
    global $user;

    $query = "SELECT id,fund_id,registered_name" .
        " FROM {fund_basic_information} where user_id = ?";
    #echo "<script>alert('".$perid."');</script>";
    $queryResult = db_query($query, array($user->uid));
    #echo "<script>alert('".$queryResult."');</script>";
        foreach ($queryResult as $links ) {
        $render_array['#markup'] .= '<div>'.l($links->registered_name,"projectstatistic/listuser/".$links->id).'</div>';
    }
    return $render_array;
}

function projects_statistic_listuser_view($fund_id) {

    $result = db_select('fund_basic_information', 'n')
        ->fields('n')
        ->condition('id',$fund_id,'=')
        ->execute();

    $name = NULL;
    foreach ($result as $a) {
        $name = $a->registered_name;
    }
    drupal_set_title($name."信息查看");
    // content variable that will be returned for display
    $html = invested_projects_information_invested_numbers_table($fund_id);$html .= "<hr/>";
    #echo "<script>alert('".$html."');</script>";
    $html .= invested_projects_information_invested_value_table($fund_id);$html .= "<hr/>";
    $html .= invested_projects_information_minimum_standard_table($fund_id);$html .= "<hr/>";
    $html .= invested_projects_information_period_statistic_table($fund_id);$html .= "<hr/>";
    $html .= invested_projects_information_location_statistic_table($fund_id);$html .= "<hr/>";
    $html .= invested_projects_information_fields_statistic_table($fund_id);

    // More coming....

    return $html;
}

// 提交基金信息表单的views&forms








//数目

function invested_projects_information_invested_numbers_form($form, &$form_state, $fund_id, $action){
    $query = db_select("invested_projects_information_invested_numbers", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id);

    $result = $query->execute();
    $rr = NULL;
    $count = 0;
    foreach ($result as $r) {
        $count++;
        $rr = $r;
    }
    $result = $rr;
    $form['tips'] = array(
        '#prefix' =>'<div id="tips">',
        '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
        '#suffix' =>'</div>',
    );

    $form['invested_number'] = array(
        '#title' => t('投资数目'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_number,
        //'#value' => $result->invested_number,
    );


    $form['invested_number_on_tsingdao'] = array(
        '#title' => t('已投青岛数'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_number_on_tsingdao,
        //'#value' => $result->invested_number_on_tsingdao,
    );


    $form['invested_number_not_on_tsingdao'] = array(
        '#title' => t('已投外地数'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_number_not_on_tsingdao,
        //'#value' => $result->invested_number_not_on_tsingdao,
    );


    $form['invested_number_on_early_tsingdao'] = array(
        '#title' => t('青岛早中期数'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_number_on_early_tsingdao,
        //'#value' => $result->invested_number_on_early_tsingdao,
    );


    $form['invested_number_not_on_early_tsingdao'] = array(
        '#title' => t('非青岛早中期数'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_number_not_on_early_tsingdao,
        //'#value' => $result->invested_number_not_on_early_tsingdao,
    );


    $form["fund_id"] = array(
        '#type' => 'hidden',
        '#value' => $fund_id,
    );
    if($count > 0){
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 2,
        );
    }else{
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 1,
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交'
    );
    return $form;
}

function invested_projects_information_invested_numbers_form_view($fund_id){
    drupal_set_title('数目');

    $render_array = array(
        '#markup' => '',
    );

    $query = db_select("invested_projects_information_invested_numbers", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id, "=");


    $result = $query->execute();
    $count = 0;
    foreach($result as $node){
        $count++;
    }

    if ($count == 0) $form = drupal_get_form('invested_projects_information_invested_numbers_form', $fund_id, 1);
    else $form = drupal_get_form('invested_projects_information_invested_numbers_form', $fund_id, 2);
    $render_array['#markup'] .= drupal_render($form);

    return $render_array;
}


function invested_projects_information_invested_numbers_form_submit($form, &$form_state){
    global $user;
    $values = $form_state["values"];
    $values["uid"] = $user->uid;

    if ($values['action'] == 1)
        $last_id = db_insert('invested_projects_information_invested_numbers')
            ->fields(array(
                'fund_id' => $values['fund_id'],

                'invested_number' => $values['invested_number'],

                'invested_number_on_tsingdao' => $values['invested_number_on_tsingdao'],

                'invested_number_not_on_tsingdao' => $values['invested_number_not_on_tsingdao'],

                'invested_number_on_early_tsingdao' => $values['invested_number_on_early_tsingdao'],

                'invested_number_not_on_early_tsingdao' => $values['invested_number_not_on_early_tsingdao'],

            ))->execute();
    else if ($values['action'] == 2)
        $last_id = db_update('invested_projects_information_invested_numbers')
            ->fields(array(

                'invested_number' => $values['invested_number'],

                'invested_number_on_tsingdao' => $values['invested_number_on_tsingdao'],

                'invested_number_not_on_tsingdao' => $values['invested_number_not_on_tsingdao'],

                'invested_number_on_early_tsingdao' => $values['invested_number_on_early_tsingdao'],

                'invested_number_not_on_early_tsingdao' => $values['invested_number_not_on_early_tsingdao'],

            ))->condition('fund_id', $values['fund_id'], '=')->execute();
    else $last_id = NULL;
    if ($last_id!=NULL) {
        $form_state['redirect'] = 'projectstatistic/listuser/'.$values['fund_id'];
    } else {
        // 数据写入失败了
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}


//金额

function invested_projects_information_invested_value_form($form, &$form_state, $fund_id, $action){
    $query = db_select("invested_projects_information_invested_value", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id);

    $result = $query->execute();
    $rr = NULL;
    $count = 0;
    foreach ($result as $r) {
        $count++;
        $rr = $r;
    }
    $result = $rr;
    $form['tips'] = array(
        '#prefix' =>'<div id="tips">',
        '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
        '#suffix' =>'</div>',
    );

    $form['invested_value'] = array(
        '#title' => t('投资金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_value,
        //'#value' => $result->invested_value,
    );


    $form['invested_value_on_tsingdao'] = array(
        '#title' => t('已投青岛金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_value_on_tsingdao,
        //'#value' => $result->invested_value_on_tsingdao,
    );


    $form['invested_value_not_on_tsingdao'] = array(
        '#title' => t('已投外地金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_value_not_on_tsingdao,
        //'#value' => $result->invested_value_not_on_tsingdao,
    );


    $form['invested_value_on_early_tsingdao'] = array(
        '#title' => t('青岛早中期金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_value_on_early_tsingdao,
        //'#value' => $result->invested_value_on_early_tsingdao,
    );


    $form['invested_value_not_on_early_tsingdao'] = array(
        '#title' => t('非青岛或非早中期金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_value_not_on_early_tsingdao,
        //'#value' => $result->invested_value_not_on_early_tsingdao,
    );


    $form["fund_id"] = array(
        '#type' => 'hidden',
        '#value' => $fund_id,
    );
    if($count > 0){
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 2,
        );
    }else{
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 1,
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交'
    );
    return $form;
}

function invested_projects_information_invested_value_form_view($fund_id){
    drupal_set_title('金额');

    $render_array = array(
        '#markup' => '',
    );

    $query = db_select("invested_projects_information_invested_value", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id, "=");


    $result = $query->execute();
    $count = 0;
    foreach($result as $node){
        $count++;
    }

    if ($count == 0) $form = drupal_get_form('invested_projects_information_invested_value_form', $fund_id, 1);
    else $form = drupal_get_form('invested_projects_information_invested_value_form', $fund_id, 2);
    $render_array['#markup'] .= drupal_render($form);

    return $render_array;
}


function invested_projects_information_invested_value_form_submit($form, &$form_state){
    global $user;
    $values = $form_state["values"];
    $values["uid"] = $user->uid;

    if ($values['action'] == 1)
        $last_id = db_insert('invested_projects_information_invested_value')
            ->fields(array(
                'fund_id' => $values['fund_id'],

                'invested_value' => $values['invested_value'],

                'invested_value_on_tsingdao' => $values['invested_value_on_tsingdao'],

                'invested_value_not_on_tsingdao' => $values['invested_value_not_on_tsingdao'],

                'invested_value_on_early_tsingdao' => $values['invested_value_on_early_tsingdao'],

                'invested_value_not_on_early_tsingdao' => $values['invested_value_not_on_early_tsingdao'],

            ))->execute();
    else if ($values['action'] == 2)
        $last_id = db_update('invested_projects_information_invested_value')
            ->fields(array(

                'invested_value' => $values['invested_value'],

                'invested_value_on_tsingdao' => $values['invested_value_on_tsingdao'],

                'invested_value_not_on_tsingdao' => $values['invested_value_not_on_tsingdao'],

                'invested_value_on_early_tsingdao' => $values['invested_value_on_early_tsingdao'],

                'invested_value_not_on_early_tsingdao' => $values['invested_value_not_on_early_tsingdao'],

            ))->condition('fund_id', $values['fund_id'], '=')->execute();
    else $last_id = NULL;
    if ($last_id!=NULL) {
        $form_state['redirect'] = 'projectstatistic/listuser/'.$values['fund_id'];
    } else {
        // 数据写入失败了
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}


//最低标准

function invested_projects_information_minimum_standard_form($form, &$form_state, $fund_id, $action){
    $query = db_select("invested_projects_information_minimum_standard", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id);

    $result = $query->execute();
    $rr = NULL;
    $count = 0;
    foreach ($result as $r) {
        $count++;
        $rr = $r;
    }
    $result = $rr;
    $form['tips'] = array(
        '#prefix' =>'<div id="tips">',
        '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
        '#suffix' =>'</div>',
    );

    $form['target_percentage_on_early_tsingdao'] = array(
        '#title' => t('应投资青岛早中期比例'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->target_percentage_on_early_tsingdao,
        //'#value' => $result->target_percentage_on_early_tsingdao,
    );


    $form['target_value'] = array(
        '#title' => t('应投资额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->target_value,
        //'#value' => $result->target_value,
    );


    $form['is_fulfilled'] = array(
        '#title' => t('是否已投够'),


        '#type' => 'checkbox',


        '#required' => TRUE,
        '#markup' => $result->is_fulfilled,
        //'#value' => $result->is_fulfilled,
    );


    $form['non_restrained_value'] = array(
        '#title' => t('非限制金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->non_restrained_value,
        //'#value' => $result->non_restrained_value,
    );


    $form['is_overflow'] = array(
        '#title' => t('是否已超限制'),


        '#type' => 'checkbox',


        '#required' => TRUE,
        '#markup' => $result->is_overflow,
        //'#value' => $result->is_overflow,
    );


    $form["fund_id"] = array(
        '#type' => 'hidden',
        '#value' => $fund_id,
    );
    if($count > 0){
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 2,
        );
    }else{
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 1,
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交'
    );
    return $form;
}

function invested_projects_information_minimum_standard_form_view($fund_id){
    drupal_set_title('最低标准');

    $render_array = array(
        '#markup' => '',
    );

    $query = db_select("invested_projects_information_minimum_standard", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id, "=");


    $result = $query->execute();
    $count = 0;
    foreach($result as $node){
        $count++;
    }

    if ($count == 0) $form = drupal_get_form('invested_projects_information_minimum_standard_form', $fund_id, 1);
    else $form = drupal_get_form('invested_projects_information_minimum_standard_form', $fund_id, 2);
    $render_array['#markup'] .= drupal_render($form);

    return $render_array;
}


function invested_projects_information_minimum_standard_form_submit($form, &$form_state){
    global $user;
    $values = $form_state["values"];
    $values["uid"] = $user->uid;

    if ($values['action'] == 1)
        $last_id = db_insert('invested_projects_information_minimum_standard')
            ->fields(array(
                'fund_id' => $values['fund_id'],

                'target_percentage_on_early_tsingdao' => $values['target_percentage_on_early_tsingdao'],

                'target_value' => $values['target_value'],

                'is_fulfilled' => $values['is_fulfilled'],

                'non_restrained_value' => $values['non_restrained_value'],

                'is_overflow' => $values['is_overflow'],

            ))->execute();
    else if ($values['action'] == 2)
        $last_id = db_update('invested_projects_information_minimum_standard')
            ->fields(array(

                'target_percentage_on_early_tsingdao' => $values['target_percentage_on_early_tsingdao'],

                'target_value' => $values['target_value'],

                'is_fulfilled' => $values['is_fulfilled'],

                'non_restrained_value' => $values['non_restrained_value'],

                'is_overflow' => $values['is_overflow'],

            ))->condition('fund_id', $values['fund_id'], '=')->execute();
    else $last_id = NULL;
    if ($last_id!=NULL) {
        $form_state['redirect'] = 'projectstatistic/listuser/'.$values['fund_id'];
    } else {
        // 数据写入失败了
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}


//时期

function invested_projects_information_period_statistic_form($form, &$form_state, $fund_id, $action){
    $query = db_select("invested_projects_information_period_statistic", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id);

    $result = $query->execute();
    $rr = NULL;
    $count = 0;
    foreach ($result as $r) {
        $count++;
        $rr = $r;
    }
    $result = $rr;
    $form['tips'] = array(
        '#prefix' =>'<div id="tips">',
        '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
        '#suffix' =>'</div>',
    );

    $form['target_percentage_on_early_tsingdao'] = array(
        '#title' => t('应投资青岛早中期比例'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->target_percentage_on_early_tsingdao,
        //'#value' => $result->target_percentage_on_early_tsingdao,
    );


    $form['target_value'] = array(
        '#title' => t('应投资额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->target_value,
        //'#value' => $result->target_value,
    );


    $form['is_fulfilled'] = array(
        '#title' => t('是否已投够'),


        '#type' => 'checkbox',


        '#required' => TRUE,
        '#markup' => $result->is_fulfilled,
        //'#value' => $result->is_fulfilled,
    );


    $form['value_surplus'] = array(
        '#title' => t('超过数额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->value_surplus,
        //'#value' => $result->value_surplus,
    );


    $form['non_restrained_value'] = array(
        '#title' => t('非限制金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->non_restrained_value,
        //'#value' => $result->non_restrained_value,
    );


    $form['is_overflow'] = array(
        '#title' => t('是否已超限制'),


        '#type' => 'checkbox',


        '#required' => TRUE,
        '#markup' => $result->is_overflow,
        //'#value' => $result->is_overflow,
    );


    $form["fund_id"] = array(
        '#type' => 'hidden',
        '#value' => $fund_id,
    );
    if($count > 0){
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 2,
        );
    }else{
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 1,
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交'
    );
    return $form;
}

function invested_projects_information_period_statistic_form_view($fund_id){
    drupal_set_title('时期');

    $render_array = array(
        '#markup' => '',
    );

    $query = db_select("invested_projects_information_period_statistic", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id, "=");


    $result = $query->execute();
    $count = 0;
    foreach($result as $node){
        $count++;
    }

    if ($count == 0) $form = drupal_get_form('invested_projects_information_period_statistic_form', $fund_id, 1);
    else $form = drupal_get_form('invested_projects_information_period_statistic_form', $fund_id, 2);
    $render_array['#markup'] .= drupal_render($form);

    return $render_array;
}


function invested_projects_information_period_statistic_form_submit($form, &$form_state){
    global $user;
    $values = $form_state["values"];
    $values["uid"] = $user->uid;

    if ($values['action'] == 1)
        $last_id = db_insert('invested_projects_information_period_statistic')
            ->fields(array(
                'fund_id' => $values['fund_id'],

                'target_percentage_on_early_tsingdao' => $values['target_percentage_on_early_tsingdao'],

                'target_value' => $values['target_value'],

                'is_fulfilled' => $values['is_fulfilled'],

                'value_surplus' => $values['value_surplus'],

                'non_restrained_value' => $values['non_restrained_value'],

                'is_overflow' => $values['is_overflow'],

            ))->execute();
    else if ($values['action'] == 2)
        $last_id = db_update('invested_projects_information_period_statistic')
            ->fields(array(

                'target_percentage_on_early_tsingdao' => $values['target_percentage_on_early_tsingdao'],

                'target_value' => $values['target_value'],

                'is_fulfilled' => $values['is_fulfilled'],

                'value_surplus' => $values['value_surplus'],

                'non_restrained_value' => $values['non_restrained_value'],

                'is_overflow' => $values['is_overflow'],

            ))->condition('fund_id', $values['fund_id'], '=')->execute();
    else $last_id = NULL;
    if ($last_id!=NULL) {
        $form_state['redirect'] = 'projectstatistic/listuser/'.$values['fund_id'];
    } else {
        // 数据写入失败了
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}


//地域

function invested_projects_information_location_statistic_form($form, &$form_state, $fund_id, $action){
    $query = db_select("invested_projects_information_location_statistic", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id);

    $result = $query->execute();
    $rr = NULL;
    $count = 0;
    foreach ($result as $r) {
        $count++;
        $rr = $r;
    }
    $result = $rr;
    $form['tips'] = array(
        '#prefix' =>'<div id="tips">',
        '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
        '#suffix' =>'</div>',
    );

    $form['target_percentage_on_tsingdao'] = array(
        '#title' => t('应投资青岛比例'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->target_percentage_on_tsingdao,
        //'#value' => $result->target_percentage_on_tsingdao,
    );


    $form['target_value'] = array(
        '#title' => t('应投资额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->target_value,
        //'#value' => $result->target_value,
    );


    $form['location_offset_value'] = array(
        '#title' => t('地域冲抵金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->location_offset_value,
        //'#value' => $result->location_offset_value,
    );


    $form['period_offset_value'] = array(
        '#title' => t('时期冲抵金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->period_offset_value,
        //'#value' => $result->period_offset_value,
    );


    $form['actual_target_value'] = array(
        '#title' => t('实际应投金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->actual_target_value,
        //'#value' => $result->actual_target_value,
    );


    $form['is_fulfilled'] = array(
        '#title' => t('是否已投够'),


        '#type' => 'checkbox',


        '#required' => TRUE,
        '#markup' => $result->is_fulfilled,
        //'#value' => $result->is_fulfilled,
    );


    $form['restrained_value'] = array(
        '#title' => t('限制外金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->restrained_value,
        //'#value' => $result->restrained_value,
    );


    $form['is_overflow'] = array(
        '#title' => t('是否已超限制'),


        '#type' => 'checkbox',


        '#required' => TRUE,
        '#markup' => $result->is_overflow,
        //'#value' => $result->is_overflow,
    );


    $form['value_surplus'] = array(
        '#title' => t('超过数额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->value_surplus,
        //'#value' => $result->value_surplus,
    );


    $form["fund_id"] = array(
        '#type' => 'hidden',
        '#value' => $fund_id,
    );
    if($count > 0){
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 2,
        );
    }else{
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 1,
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交'
    );
    return $form;
}

function invested_projects_information_location_statistic_form_view($fund_id){
    drupal_set_title('地域');

    $render_array = array(
        '#markup' => '',
    );

    $query = db_select("invested_projects_information_location_statistic", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id, "=");


    $result = $query->execute();
    $count = 0;
    foreach($result as $node){
        $count++;
    }

    if ($count == 0) $form = drupal_get_form('invested_projects_information_location_statistic_form', $fund_id, 1);
    else $form = drupal_get_form('invested_projects_information_location_statistic_form', $fund_id, 2);
    $render_array['#markup'] .= drupal_render($form);

    return $render_array;
}


function invested_projects_information_location_statistic_form_submit($form, &$form_state){
    global $user;
    $values = $form_state["values"];
    $values["uid"] = $user->uid;

    if ($values['action'] == 1)
        $last_id = db_insert('invested_projects_information_location_statistic')
            ->fields(array(
                'fund_id' => $values['fund_id'],

                'target_percentage_on_tsingdao' => $values['target_percentage_on_tsingdao'],

                'target_value' => $values['target_value'],

                'location_offset_value' => $values['location_offset_value'],

                'period_offset_value' => $values['period_offset_value'],

                'actual_target_value' => $values['actual_target_value'],

                'is_fulfilled' => $values['is_fulfilled'],

                'restrained_value' => $values['restrained_value'],

                'is_overflow' => $values['is_overflow'],

                'value_surplus' => $values['value_surplus'],

            ))->execute();
    else if ($values['action'] == 2)
        $last_id = db_update('invested_projects_information_location_statistic')
            ->fields(array(

                'target_percentage_on_tsingdao' => $values['target_percentage_on_tsingdao'],

                'target_value' => $values['target_value'],

                'location_offset_value' => $values['location_offset_value'],

                'period_offset_value' => $values['period_offset_value'],

                'actual_target_value' => $values['actual_target_value'],

                'is_fulfilled' => $values['is_fulfilled'],

                'restrained_value' => $values['restrained_value'],

                'is_overflow' => $values['is_overflow'],

                'value_surplus' => $values['value_surplus'],

            ))->condition('fund_id', $values['fund_id'], '=')->execute();
    else $last_id = NULL;
    if ($last_id!=NULL) {
        $form_state['redirect'] = 'projectstatistic/listuser/'.$values['fund_id'];
    } else {
        // 数据写入失败了
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}


//领域

function invested_projects_information_fields_statistic_form($form, &$form_state, $fund_id, $action){
    $query = db_select("invested_projects_information_fields_statistic", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id);

    $result = $query->execute();
    $rr = NULL;
    $count = 0;
    foreach ($result as $r) {
        $count++;
        $rr = $r;
    }
    $result = $rr;
    $form['tips'] = array(
        '#prefix' =>'<div id="tips">',
        '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
        '#suffix' =>'</div>',
    );

    $form['target_percentage_on_fields'] = array(
        '#title' => t('应投资领域比例'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->target_percentage_on_fields,
        //'#value' => $result->target_percentage_on_fields,
    );


    $form['target_value'] = array(
        '#title' => t('应投资额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->target_value,
        //'#value' => $result->target_value,
    );


    $form['invested_value'] = array(
        '#title' => t('已投资该领域额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_value,
        //'#value' => $result->invested_value,
    );


    $form['is_fulfilled'] = array(
        '#title' => t('是否已投够'),


        '#type' => 'checkbox',


        '#required' => TRUE,
        '#markup' => $result->is_fulfilled,
        //'#value' => $result->is_fulfilled,
    );


    $form['non_restrained_value'] = array(
        '#title' => t('非限制金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->non_restrained_value,
        //'#value' => $result->non_restrained_value,
    );


    $form['invested_non_restrained_value'] = array(
        '#title' => t('已投非限金额'),


        '#type' => 'textfield',


        '#required' => TRUE,
        '#markup' => $result->invested_non_restrained_value,
        //'#value' => $result->invested_non_restrained_value,
    );


    $form['is_overflow'] = array(
        '#title' => t('是否已超限制'),


        '#type' => 'checkbox',


        '#required' => TRUE,
        '#markup' => $result->is_overflow,
        //'#value' => $result->is_overflow,
    );


    $form['fields'] = array(
        '#title' => t('领域'),


        '#type' => 'text',


        '#required' => TRUE,
        '#markup' => $result->fields,
        //'#value' => $result->fields,
    );


    $form["fund_id"] = array(
        '#type' => 'hidden',
        '#value' => $fund_id,
    );
    if($count > 0){
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 2,
        );
    }else{
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 1,
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交'
    );
    return $form;
}

function invested_projects_information_fields_statistic_form_view($fund_id){
    drupal_set_title('领域');

    $render_array = array(
        '#markup' => '',
    );

    $query = db_select("invested_projects_information_fields_statistic", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.fund_id', $fund_id, "=");


    $result = $query->execute();
    $count = 0;
    foreach($result as $node){
        $count++;
    }

    if ($count == 0) $form = drupal_get_form('invested_projects_information_fields_statistic_form', $fund_id, 1);
    else $form = drupal_get_form('invested_projects_information_fields_statistic_form', $fund_id, 2);
    $render_array['#markup'] .= drupal_render($form);

    return $render_array;
}


function invested_projects_information_fields_statistic_form_submit($form, &$form_state){
    global $user;
    $values = $form_state["values"];
    $values["uid"] = $user->uid;

    if ($values['action'] == 1)
        $last_id = db_insert('invested_projects_information_fields_statistic')
            ->fields(array(
                'fund_id' => $values['fund_id'],

                'target_percentage_on_fields' => $values['target_percentage_on_fields'],

                'target_value' => $values['target_value'],

                'invested_value' => $values['invested_value'],

                'is_fulfilled' => $values['is_fulfilled'],

                'non_restrained_value' => $values['non_restrained_value'],

                'invested_non_restrained_value' => $values['invested_non_restrained_value'],

                'is_overflow' => $values['is_overflow'],

                'fields' => $values['fields'],

            ))->execute();
    else if ($values['action'] == 2)
        $last_id = db_update('invested_projects_information_fields_statistic')
            ->fields(array(

                'target_percentage_on_fields' => $values['target_percentage_on_fields'],

                'target_value' => $values['target_value'],

                'invested_value' => $values['invested_value'],

                'is_fulfilled' => $values['is_fulfilled'],

                'non_restrained_value' => $values['non_restrained_value'],

                'invested_non_restrained_value' => $values['invested_non_restrained_value'],

                'is_overflow' => $values['is_overflow'],

                'fields' => $values['fields'],

            ))->condition('fund_id', $values['fund_id'], '=')->execute();
    else $last_id = NULL;
    if ($last_id!=NULL) {
        $form_state['redirect'] = 'projectstatistic/listuser/'.$values['fund_id'];
    } else {
        // 数据写入失败了
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}


// 致谢页面 by jy
function projects_statistic_thanks() {
    drupal_set_title('基金信息管理');
    $render_array = array(
        '#markup' => '',
    );

    $render_array['#markup'] .= '<div>提交成功！</div>';
    $render_array['#markup'] .= '<div>'.l("点此返回基金管理主页面","projectstatistic/main/").'</div>';

    return $render_array;
}

?>